package malus.shaofeng.com.beiwomusic.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;

import java.util.List;

import malus.shaofeng.com.beiwomusic.R;
import malus.shaofeng.com.beiwomusic.activity.NewStoryDetailActivity;
import malus.shaofeng.com.beiwomusic.adapter.CollectionAadapter;
import malus.shaofeng.com.beiwomusic.model.NewStory;

/**
 * A simple {@link Fragment} subclass.
 */
public class SpaceMineConnectionFragment extends Fragment implements AdapterView.OnItemClickListener {
    private DbUtils dbUtils;
    private List<NewStory> newStoryList;

    public SpaceMineConnectionFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View ret = inflater.inflate(R.layout.fragment_space_mine_connection, container, false);
        ListView listView = (ListView)ret.findViewById(R.id.myCollection_listView);

        dbUtils = DbUtils.create(getActivity(), "storyFavourite.db");
        dbUtils.configAllowTransaction(true);
        dbUtils.configDebug(true);

        Selector selector = Selector.from(NewStory.class);
        try {
            newStoryList = dbUtils.findAll(selector);

            CollectionAadapter adapter = new CollectionAadapter(getActivity(), newStoryList);
            listView.setAdapter(adapter);
       //     adapter.notifyDataSetChanged();

            listView.setOnItemClickListener(this);
            Log.d("list", newStoryList.size()+"");

        } catch (DbException e) {
            e.printStackTrace();
        }

        return ret;
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        NewStory story = newStoryList.get(position);
        Intent intent=new Intent(getActivity(), NewStoryDetailActivity.class);
        intent.putExtra("position",position);
        intent.putExtra("story",story);
        startActivity(intent);
    }
}
